<!DOCTYPE html>
<html>
<head>
    <title>门诊查询--中软高科-2015</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="../Css/style.css"/>
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }

        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>

</head>
<body>

<form action="index.html" method="post" class="definewidth m20" id="myForm">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">病历号：</td>
            <td><input type="text" id="uid" name="pname" value=""/></td>

            <td width="10%" class="tableleft">主治医生：</td>
            <td><input type="text" id="doctorName" name="pname" value=""/></td>

            <td width="10%" class="tableleft">科室：</td>
            <td><input type="text" id="dept" name="pname" value=""/></td>
        </tr>
        <tr>

            <td width="10%" class="tableleft">挂号时间：</td>

            <td colspan="5">
                <input type="text" name="pname" value=""/>&nbsp;至&nbsp;<input type="text" name="pname" value=""/>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <button type="button" class="btn btn-primary" onclick="toFind()">查询</button>
                <button type="button" class="btn btn-primary" onclick="findList()">清空</button>

            </td>
        </tr>
    </table>
</form>

<table class="table table-bordered table-hover definewidth m10">
    <thead>
    <tr>
        <th><input type="checkbox" id="checkall" onChange="checkall();"></th>
        <th>门诊编号</th>
        <th>主治医生</th>
        <th>挂号时间</th>
        <th>挂号科室</th>
        <th>状态</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody id="tbody">

    </tbody>


</table>

<table class="table table-bordered table-hover definewidth m10">
    <tr>
        <th colspan="5">
            <div class="inline pull-right page" id="a">
                <!--          <a href='#' >第一页</a> <a href='#'>上一页</a>     <span class='current'>1</span><a href='#'>2</a><a href='/chinapost/index.php?m=Label&a=index&p=3'>3</a><a href='#'>4</a><a href='#'>5</a>  <a href='#' >下一页</a> <a href='#' >最后一页</a>-->
                &nbsp;&nbsp;&nbsp;
            </div>
            <div>
                <button type="button" class="btn btn-success" id="newNav">门诊挂号</button>&nbsp;&nbsp;&nbsp;
                <button type="button" class="btn btn-success" id="delPro" onClick="delAll();">退号</button>&nbsp;&nbsp;&nbsp;
                <button type="button" class="btn btn-success">导出Excel</button>


            </div>

        </th>
    </tr>
</table>
<script type="text/javascript">
    $(function () {
        $('#newNav').click(function () {
            window.location.href = "add.html";
        });
    });

    //定义分页函数
    function fenye(vo) {
        let pageSize = vo.pageSize;
        $("#a").html("<div id=\"b\" name=\"pageDiv\">\n" +
            "\n" +
            "</div>");
        $("#a").append("\t\t  &nbsp;&nbsp;&nbsp;共<span class='current'> " + vo.total + " </span>条记录<span class='current'> " + vo.pages + " </span>页\n");

        if (vo.pageNum != 1)
            $("#b").append("<span name=\"pageSpan\" id=\"first_page\"><a href=\"javascript:void(0)\" onclick=\"findList("+$("#uid").val()+","+$("#doctorName").val()+","+$("#dept").val()+",1," + pageSize + ")\">第一页</a></span>");

        if (vo.hasPreviousPage == true) {
            $("#b").append("<span name=\"pageSpan\" id=\"pre_page\"><a href=\"javascript:void(0)\" onclick=\"findList("+$("#uid").val()+","+$("#doctorName").val()+","+$("#dept").val()+"," + (vo.pageNum - 1) + "," + pageSize + ")\">上一页</a></span>");
        }

        for (let i = 0; i < vo.navigatepageNums.length; i++) {
            let index = vo.navigatepageNums[i];
            if (vo.pageNum == index) {
                $("#b").append("<span name=\"pageSpan\" id=\"page1\"><a href='javascript:void(0)' style=\"color: #ff0000;\" onclick=\"findList("+$("#uid").val()+","+$("#doctorName").val()+","+$("#dept").val()+"," + index + "," + pageSize + ")\">" + index + "</a></span>");
            } else {
                $("#b").append("<span name=\"pageSpan\" id=\"page1\"><a href='javascript:void(0)' onclick=\"findList("+$("#uid").val()+","+$("#doctorName").val()+","+$("#dept").val()+"," + index + "," + pageSize + ")\">" + index + "</a></span>");
            }

        }

        if (vo.hasNextPage == true) {
            $("#b").append("<span name=\"pageSpan\" id=\"next_page\"><a href='javascript:void(0)' onclick=\"findList("+$("#uid").val()+","+$("#doctorName").val()+","+$("#dept").val()+"," + (vo.pageNum + 1) + "," + pageSize + ")\">下一页</a></span>");
        }

        if (vo.isLastPage == false) {
            $("#b").append("<span name=\"pageSpan\" id=\"last_page\"><a href='javascript:void(0)' onclick=\"findList("+$("#uid").val()+","+$("#doctorName").val()+","+$("#dept").val()+"," + (vo.pages) + "," + pageSize + ")\">最后一页</a></span>");
        }

    }

    //
    // function findAll(){
    //     $.ajax({
    //         url: "/patient/detail",
    //         method:"GET",
    //         dataType:"json",
    //         success: function(vo){
    //             var list = vo.list;
    //             var str="";
    //             for(var i=0;i<list.length;i++){
    //                 if(list[i].status==0){
    //                     var status="已挂号";
    //                 }else if(list[i].status==1){
    //                     var status="已住院";
    //                 }
    //                 var time = timestampToTime(list[i].time);
    //                 str+="<tr >\n" +
    //                     "        <td style=\"vertical-align:middle;\"><input type=\"checkbox\" name=\"check\" value=\""+list[i].id+"\"></td>\n" +
    //                     "        <td style=\"vertical-align:middle;\">"+list[i].id+"</td>\n" +
    //                     "        <td style=\"vertical-align:middle;\">"+list[i].doctor+"</td>\n" +
    //                     "        <td style=\"vertical-align:middle;\">"+time+"</td>\n" +
    //                     "        <td style=\"vertical-align:middle;\">"+list[i].patientDept+"</td>\n" +
    //                     "        <td style=\"vertical-align:middle;\">"+status+"</td>\n" +
    //                     "        <td style=\"vertical-align:middle;\"><a href='#' onclick=\"lookById("+list[i].id+")\">详情>>></a>&nbsp;&nbsp;&nbsp;<a href='#' onclick=\"editById("+list[i].id+")\">更改</a>&nbsp;&nbsp;&nbsp;" +
    //                     "<a href='' onclick=\"deleteById("+list[i].id+")\">退号</a></td>\n" +
    //                     "    </tr>"
    //
    //             }
    //             $("#tbody").html(str);
    //         }
    //     })
    // }
    //
    // findAll();

    function deleteById(id) {
        var bol = confirm("你确定要删除吗");
        if (bol) {
            //删除

            $.ajax({
                url: "/patient/delete/" + id,
                method: "DELETE",
                dataType: "json",
                success: function (vo) {
                    alert(vo.msg);
                    if (vo.code == "200") {
                        findList();
                    }
                }
            })
        } else {
            return false;
        }
    }


    function timestampToTime(timestamp) {

        var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000

        Y = date.getFullYear() + '/';

        M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '/';

        D = date.getDate() + ' ';

        h = date.getHours() + ':';

        m = date.getMinutes() + ':';

        s = date.getSeconds();

        return Y + M + D;

    }

    function toFind() {
        let uid = $("#uid").val();
        let doctorName = $("#doctorName").val();
        let dept = $("#dept").val();
        findList(uid, doctorName, dept);
    }


    function findList(id, doctorName, dept, pageNum, pageSize) {

        $.ajax({
            url: "/patient/list",
            method: "GET",
            data: {
                id: id,
                doctorName: doctorName,
                dept: dept,
                pageNum: pageNum,
                pageSize: pageSize
            },
            dataType: "json",
            success: function (vo) {
                var list = vo.list;
                var str = "";
                var status = "";
                for (var i = 0; i < list.length; i++) {
                    if (list[i].status == 0) {
                        status = "已挂号";
                    } else if (list[i].status == 1) {
                        status = "已住院";
                    }
                    var time = timestampToTime(list[i].time);
                    str += "<tr >\n" +
                        "        <td style=\"vertical-align:middle;\"><input type=\"checkbox\" name=\"check\" value=\"" + list[i].id + "\"></td>\n" +
                        "        <td style=\"vertical-align:middle;\">" + list[i].id + "</td>\n" +
                        "        <td style=\"vertical-align:middle;\">" + list[i].doctor + "</td>\n" +
                        "        <td style=\"vertical-align:middle;\">" + time + "</td>\n" +
                        "        <td style=\"vertical-align:middle;\">" + list[i].patientDept + "</td>\n" +
                        "        <td style=\"vertical-align:middle;\">" + status + "</td>\n" +
                        "        <td style=\"vertical-align:middle;\"><a href='#' onclick=\"lookById(" + list[i].id + ")\">详情>>></a>&nbsp;&nbsp;&nbsp;<a href='#' onclick=\"editById(" + list[i].id + ")\">更改</a>&nbsp;&nbsp;&nbsp;" +
                        "<a href='' onclick=\"deleteById(" + list[i].id + ")\">退号</a></td>\n" +
                        "    </tr>"

                }
                $("#tbody").html(str);
                fenye(vo);
            }
        })
    }

    $(() => {
        findList();
    })

    // 共<span class='current'>vo.total</span>条记录<span class='current'> vo.pages </span>页

    function lookById(id) {
        window.location.href = "/Registration/look.html?id=" + id
    }

    function editById(id) {
        window.location.href = "/Registration/edit.html?id=" + id
    }

    function checkall() {
        var alls = document.getElementsByName("check");
        var ch = document.getElementById("checkall");
        if (ch.checked) {
            for (var i = 0; i < alls.length; i++) {
                alls[i].checked = true;
            }
        } else {
            for (var i = 0; i < alls.length; i++) {
                alls[i].checked = false;
            }
        }
    }

    function delAll() {
        var alls = document.getElementsByName("check");
        var ids = new Array();
        for (var i = 0; i < alls.length; i++) {
            if (alls[i].checked) {
                ids.push(alls[i].value);
            }
        }
        if (ids.length > 0) {
            if (confirm("确认操作?")) {
                while (ids.length > 0) {
                    let id = ids.pop();
                    $.ajax({
                        url: "/patient/delete/" + id,
                        method: "DELETE",
                        dataType: "json",
                        success: function (vo) {

                        }
                    })
                }
                alert("成功!");
                findList();
            }
        } else {
            alert("请选中要操作的项");
        }
    }

</script>
</body>
</html>
